/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpa.controller;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import jpa.controller.exceptions.IllegalOrphanException;
import jpa.controller.exceptions.NonexistentEntityException;
import jpa.entities.Cuotas;
import jpa.entities.Comprobante;
import java.util.ArrayList;
import java.util.List;
import jpa.entities.Tarjeta;
import jpa.entities.Venta;

/**
 *
 * @author Usuario
 */
public class TarjetaJpaController {

    public TarjetaJpaController() {
        emf = Persistence.createEntityManagerFactory("Sig-ejbPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tarjeta tarjeta) {
        if (tarjeta.getComprobanteList() == null) {
            tarjeta.setComprobanteList(new ArrayList<Comprobante>());
        }
        if (tarjeta.getVentaList() == null) {
            tarjeta.setVentaList(new ArrayList<Venta>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cuotas cuotas = tarjeta.getCuotas();
            if (cuotas != null) {
                cuotas = em.getReference(cuotas.getClass(), cuotas.getIdCuotas());
                tarjeta.setCuotas(cuotas);
            }
            List<Comprobante> attachedComprobanteList = new ArrayList<Comprobante>();
            for (Comprobante comprobanteListComprobanteToAttach : tarjeta.getComprobanteList()) {
                comprobanteListComprobanteToAttach = em.getReference(comprobanteListComprobanteToAttach.getClass(), comprobanteListComprobanteToAttach.getIdComprobante());
                attachedComprobanteList.add(comprobanteListComprobanteToAttach);
            }
            tarjeta.setComprobanteList(attachedComprobanteList);
            List<Venta> attachedVentaList = new ArrayList<Venta>();
            for (Venta ventaListVentaToAttach : tarjeta.getVentaList()) {
                ventaListVentaToAttach = em.getReference(ventaListVentaToAttach.getClass(), ventaListVentaToAttach.getIdVenta());
                attachedVentaList.add(ventaListVentaToAttach);
            }
            tarjeta.setVentaList(attachedVentaList);
            em.persist(tarjeta);
            if (cuotas != null) {
                Tarjeta oldTarjetaOfCuotas = cuotas.getTarjeta();
                if (oldTarjetaOfCuotas != null) {
                    oldTarjetaOfCuotas.setCuotas(null);
                    oldTarjetaOfCuotas = em.merge(oldTarjetaOfCuotas);
                }
                cuotas.setTarjeta(tarjeta);
                cuotas = em.merge(cuotas);
            }
            for (Comprobante comprobanteListComprobante : tarjeta.getComprobanteList()) {
                Tarjeta oldTarjetaOfComprobanteListComprobante = comprobanteListComprobante.getTarjeta();
                comprobanteListComprobante.setTarjeta(tarjeta);
                comprobanteListComprobante = em.merge(comprobanteListComprobante);
                if (oldTarjetaOfComprobanteListComprobante != null) {
                    oldTarjetaOfComprobanteListComprobante.getComprobanteList().remove(comprobanteListComprobante);
                    oldTarjetaOfComprobanteListComprobante = em.merge(oldTarjetaOfComprobanteListComprobante);
                }
            }
            for (Venta ventaListVenta : tarjeta.getVentaList()) {
                Tarjeta oldTarjetaOfVentaListVenta = ventaListVenta.getTarjeta();
                ventaListVenta.setTarjeta(tarjeta);
                ventaListVenta = em.merge(ventaListVenta);
                if (oldTarjetaOfVentaListVenta != null) {
                    oldTarjetaOfVentaListVenta.getVentaList().remove(ventaListVenta);
                    oldTarjetaOfVentaListVenta = em.merge(oldTarjetaOfVentaListVenta);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tarjeta tarjeta) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta persistentTarjeta = em.find(Tarjeta.class, tarjeta.getIdTarjeta());
            Cuotas cuotasOld = persistentTarjeta.getCuotas();
            Cuotas cuotasNew = tarjeta.getCuotas();
            List<Comprobante> comprobanteListOld = persistentTarjeta.getComprobanteList();
            List<Comprobante> comprobanteListNew = tarjeta.getComprobanteList();
            List<Venta> ventaListOld = persistentTarjeta.getVentaList();
            List<Venta> ventaListNew = tarjeta.getVentaList();
            List<String> illegalOrphanMessages = null;
            if (cuotasOld != null && !cuotasOld.equals(cuotasNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Cuotas " + cuotasOld + " since its tarjeta field is not nullable.");
            }
            for (Comprobante comprobanteListOldComprobante : comprobanteListOld) {
                if (!comprobanteListNew.contains(comprobanteListOldComprobante)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Comprobante " + comprobanteListOldComprobante + " since its tarjeta field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cuotasNew != null) {
                cuotasNew = em.getReference(cuotasNew.getClass(), cuotasNew.getIdCuotas());
                tarjeta.setCuotas(cuotasNew);
            }
            List<Comprobante> attachedComprobanteListNew = new ArrayList<Comprobante>();
            for (Comprobante comprobanteListNewComprobanteToAttach : comprobanteListNew) {
                comprobanteListNewComprobanteToAttach = em.getReference(comprobanteListNewComprobanteToAttach.getClass(), comprobanteListNewComprobanteToAttach.getIdComprobante());
                attachedComprobanteListNew.add(comprobanteListNewComprobanteToAttach);
            }
            comprobanteListNew = attachedComprobanteListNew;
            tarjeta.setComprobanteList(comprobanteListNew);
            List<Venta> attachedVentaListNew = new ArrayList<Venta>();
            for (Venta ventaListNewVentaToAttach : ventaListNew) {
                ventaListNewVentaToAttach = em.getReference(ventaListNewVentaToAttach.getClass(), ventaListNewVentaToAttach.getIdVenta());
                attachedVentaListNew.add(ventaListNewVentaToAttach);
            }
            ventaListNew = attachedVentaListNew;
            tarjeta.setVentaList(ventaListNew);
            tarjeta = em.merge(tarjeta);
            if (cuotasNew != null && !cuotasNew.equals(cuotasOld)) {
                Tarjeta oldTarjetaOfCuotas = cuotasNew.getTarjeta();
                if (oldTarjetaOfCuotas != null) {
                    oldTarjetaOfCuotas.setCuotas(null);
                    oldTarjetaOfCuotas = em.merge(oldTarjetaOfCuotas);
                }
                cuotasNew.setTarjeta(tarjeta);
                cuotasNew = em.merge(cuotasNew);
            }
            for (Comprobante comprobanteListNewComprobante : comprobanteListNew) {
                if (!comprobanteListOld.contains(comprobanteListNewComprobante)) {
                    Tarjeta oldTarjetaOfComprobanteListNewComprobante = comprobanteListNewComprobante.getTarjeta();
                    comprobanteListNewComprobante.setTarjeta(tarjeta);
                    comprobanteListNewComprobante = em.merge(comprobanteListNewComprobante);
                    if (oldTarjetaOfComprobanteListNewComprobante != null && !oldTarjetaOfComprobanteListNewComprobante.equals(tarjeta)) {
                        oldTarjetaOfComprobanteListNewComprobante.getComprobanteList().remove(comprobanteListNewComprobante);
                        oldTarjetaOfComprobanteListNewComprobante = em.merge(oldTarjetaOfComprobanteListNewComprobante);
                    }
                }
            }
            for (Venta ventaListOldVenta : ventaListOld) {
                if (!ventaListNew.contains(ventaListOldVenta)) {
                    ventaListOldVenta.setTarjeta(null);
                    ventaListOldVenta = em.merge(ventaListOldVenta);
                }
            }
            for (Venta ventaListNewVenta : ventaListNew) {
                if (!ventaListOld.contains(ventaListNewVenta)) {
                    Tarjeta oldTarjetaOfVentaListNewVenta = ventaListNewVenta.getTarjeta();
                    ventaListNewVenta.setTarjeta(tarjeta);
                    ventaListNewVenta = em.merge(ventaListNewVenta);
                    if (oldTarjetaOfVentaListNewVenta != null && !oldTarjetaOfVentaListNewVenta.equals(tarjeta)) {
                        oldTarjetaOfVentaListNewVenta.getVentaList().remove(ventaListNewVenta);
                        oldTarjetaOfVentaListNewVenta = em.merge(oldTarjetaOfVentaListNewVenta);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tarjeta.getIdTarjeta();
                if (findTarjeta(id) == null) {
                    throw new NonexistentEntityException("The tarjeta with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta tarjeta;
            try {
                tarjeta = em.getReference(Tarjeta.class, id);
                tarjeta.getIdTarjeta();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tarjeta with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Cuotas cuotasOrphanCheck = tarjeta.getCuotas();
            if (cuotasOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tarjeta (" + tarjeta + ") cannot be destroyed since the Cuotas " + cuotasOrphanCheck + " in its cuotas field has a non-nullable tarjeta field.");
            }
            List<Comprobante> comprobanteListOrphanCheck = tarjeta.getComprobanteList();
            for (Comprobante comprobanteListOrphanCheckComprobante : comprobanteListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tarjeta (" + tarjeta + ") cannot be destroyed since the Comprobante " + comprobanteListOrphanCheckComprobante + " in its comprobanteList field has a non-nullable tarjeta field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Venta> ventaList = tarjeta.getVentaList();
            for (Venta ventaListVenta : ventaList) {
                ventaListVenta.setTarjeta(null);
                ventaListVenta = em.merge(ventaListVenta);
            }
            em.remove(tarjeta);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tarjeta> findTarjetaEntities() {
        return findTarjetaEntities(true, -1, -1);
    }

    public List<Tarjeta> findTarjetaEntities(int maxResults, int firstResult) {
        return findTarjetaEntities(false, maxResults, firstResult);
    }

    private List<Tarjeta> findTarjetaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tarjeta.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tarjeta findTarjeta(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tarjeta.class, id);
        } finally {
            em.close();
        }
    }

    public int getTarjetaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tarjeta> rt = cq.from(Tarjeta.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
